<?php
namespace System\Model;
use Think\Model;
/**
 * huhaiqin，2023.03.20
 * 数据库备份操作
 */
class BackupModel extends Model{

    // 检测权限
    private function _check_auth(){
        if(!$_SESSION['loginUser']){
            _apiReturn(0,'请先登录~');
        }
        if($_SESSION['loginUser']['role'] != 'admin'){
            _apiReturn(0,'您没有权限执行该操作~');
        }
    }

    /**
     * huhaiqin,2023.03.20
     * 根据ID获取详细数据
     */
    public function _find($id = 0){
        if(!$id){
            _apiReturn(0,'操作ID不能为空');
        }
        $data = $this->find($id);
        return $data;
    }
    
    /**
     * huhaiqin,2023.03.20
     * 备份数据库
     */
    public function _add(){
        $this->_check_auth();
        $backup_obj = new \System\Org\Backup(); //创建数据库备份对象
        $data = $backup_obj->backups();         //备份数据库，并返回备份的教过，['file_size'=>$file_size,'file_name'=>$filename]
        if($data){
            $data['uid'] = $_SESSION['loginUser']['id']; //记录操作的用户ID
            $data['create_time'] = time();      // 创建时间
            if($this->add($data)){
                _apiReturn(1,'success');
            }else{
                _apiReturn(0,'数据库备份失败~');
            }
        }else{
            _apiReturn(0,'数据库备份失败~');
        }
    }


    /**
     * huhaiqin,2023.03.20
     * 物理删除，包括数据库记录的删除和备份文件的删除
     */
    public function _del($id = 0){
        $this->_check_auth();
        if(!$id){
            _apiReturn(0,'缺少参数');
        }
        $check = $this->find($id);
        if($this->where('id='.$id)->delete()){
            $backup_obj = new \System\ORG\Backup(); //创建数据库备份对象
            $backup_obj->del_file($check['file_name']); //删除备份文件
            _apiReturn(1,'success');
        }else{
            _apiReturn(0,'error');
        }
    }


    /**
     * huhaiqin,2023.03.20
     * 数据接口
     */
    public function _query($p = 1, $row = 10, $query = ''){
        $sql = 'SELECT A.*,C.username,C.avatar,C.realname,C.role FROM mini_backup A LEFT JOIN mini_users C ON C.id = A.uid WHERE 1=1 ';
        $sqlcount = 'SELECT COUNT(A.`id`) AS tp_count FROM mini_backup A LEFT JOIN mini_users C ON C.id = A.uid WHERE 1=1 ';
        if ($query) {
            $sql .= $query;
            $sqlcount .= $query;
        }
        $sql .= ' ORDER BY A.id DESC ';
        $sql .= ' LIMIT '.($p - 1) * $row.','.$row;
        $total = $this->query($sqlcount);
        $data['data'] = $this->query($sql);
        $data['page']['total'] = $total[0]['tp_count'];
        $data['page']['cur'] = $p;
        $data['page']['row'] = $row;
        // $data['sql'] = $sql;
        // dump($data);
        // exit();
        return $data;
    }

    /**
     * huhaiqin，2023.03.20
     * 根据记录id下载数据库备份文件
     */
    public function download_by_id($id = 0){
        $this->_check_auth();
        if(!$id){
            _apiReturn(0,'下载ID不能为空！');
        }
        $data = $this->find($id);
        if($data){
            $backup_obj = new \System\ORG\Backup(); //创建数据库备份对象
            $backup_obj->download($data['file_name'],$data['file_size']);  // 加载文件
        }else{
            return false;
        }
    }

}